

#include <iostream>

#include <basic_packet.hpp>
#include <byteorder.hpp>

using namespace xbase;

def_packet_4(unsigned long long,
	     long,
	     short int,
	     char,
	     trailer);

int main()
{
	trailer t0;

	std::cout << "sizeof trailer: " << sizeof(trailer) << std::endl;
	
	packet_value<0>(t0) = 0xf8f7f6f5f4f3f2f0ull;
	packet_value<1>(t0) = 0x93929190l;
	packet_value<2>(t0) = 0x6160;
	packet_value<3>(t0) = 'x';
	//packet_value<4>(t0) = 9;

	std::cout << "t0: " << std::hex << packet_value<0>(t0)
		  << ", " << std::hex << packet_value<1>(t0)
		  << ", " << std::hex << packet_value<2>(t0)
		  << ", " << std::dec << packet_value<3>(t0)
		  << std::endl;

	char array[1024];
	char * buffer = array;
	basic_packet_ostream<trailer, kb::network_byteorder> out(buffer, 1024);
	out << t0;
	if(! out.good())
	{
		std::cout << "t0 save error" << std::endl;
		return 1;
	}

	std::cout << "buffer: " << std::hex;
	const char * buffer_end = buffer + out.saved_size();
	for(const char *p = buffer; p != buffer_end; ++p)
	{
		std::cout << (unsigned int)(unsigned char)*p << ' ';
	}
	std::cout << std::dec << std::endl;

	trailer t1;
	basic_packet_istream<trailer, kb::network_byteorder> in(buffer, 1024);
	in >> t1;
	if(! in.good())
	{
		std::cout << "t1 load error" << std::endl;
		return 1;
	}

	std::cout << "t1: " << std::hex << packet_value<0>(t1)
		  << ", " << std::hex << packet_value<1>(t1)
		  << ", " << std::hex << packet_value<2>(t1)
		  << ", " << std::dec << packet_value<3>(t1)
		  << std::endl;

	assert(in.loaded_size() == out.saved_size());

	return 0;
}
